#version 450 compatibility



layout(quads, equal_spacing, ccw) in;


in vec4 Normal_ps[];
in vec2 TCoord_ps[];
in vec4 Position_ps[];

out vec4 Normal;
out vec2 TCoord;

vec3 interpolate3D(vec3 v0, vec3 v1, vec3 v2) {
    return vec3(gl_TessCoord.x) * v0 + vec3(gl_TessCoord.y) * v1 + vec3(gl_TessCoord.z) * v2;
} 
/*vec3 interpolate4D(vec3 v0, vec3 v1, vec3 v2, vec3 v3) {
    return vec3(gl_TessCoord.x) * v0 + vec3(gl_TessCoord.y) * v1 + vec3(gl_TessCoord.z) * v2 + vec3(gl_TessCoord.z) * v3;
}*/

void main(){
    float u = gl_TessCoord.x;
    float omu = 1-u;
    float v = gl_TessCoord.y;
    float omv = 1-v;

    vec4 vertex = omu*omv * gl_in[0].gl_Position  
                    +  u*omv * gl_in[1].gl_Position  
                    +  u*v*gl_in[2].gl_Position  
                    +  omu*v*gl_in[3].gl_Position;

    /*vec4 vertex = u*v * gl_in[0].gl_Position  
                    +  v*omu * gl_in[1].gl_Position  
                    +  omu*omv*gl_in[2].gl_Position  
                    +  u*omv*gl_in[3].gl_Position;*/
    //vec4 vertex.xyz = gl_in[0].gl_Position*gl_TessCoord.x + gl_in[1].gl_Position*gl_TessCoord.y + gl_in[2].gl_Position*gl_TessCoord.z;
    /*vec4 vertex = omu*omv * Position_ps[0]  +  u*omv * Position_ps[1]  
                    +  u*v*Position_ps[2]  +  omu*v*Position_ps[3];*/
    //vertex.xyz = gl_TessCoord;
    //vertex.xyz = normalize(vertex.xyz);
    
    
    Normal.xyz = omu*omv*Normal_ps[0].xyz + u*omv*Normal_ps[1].xyz + u*v*Normal_ps[2].xyz + omu*v*Normal_ps[3].xyz;
    vertex.xyz += normalize(Normal.xyz) * Normal.w;

    vertex.w = 1.0;
    gl_Position = gl_ProjectionMatrix * vertex;

    //Normal.xyz = interpolate3D(Normal_ps[0].xyz, Normal_ps[1].xyz, Normal_ps[2].xyz);
    TCoord = gl_TessCoord.xy;

}



